package com.woupla.account;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.New;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.solder.logging.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.woupla.model.Rdv;
import com.woupla.model.User;
import com.woupla.rdv.RdvEJB;

/**
 * 
 */

/**
 * @author Dimitri Lebel
 * 
 */
@Named
@ViewScoped
public class profilController implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4445950074774092185L;
	@Inject
	private UserEJB userEJB;
	@New
	private User user;
	@Inject
	private Logger log;
	@Inject
	private CurrentUserManager currentUserManager;
	@Inject
	private RdvEJB rdvEJB;

	private LazyDataModel<Rdv> rdvs;
	private Rdv selectedRdv;

	private boolean isChanged;

	public void updateUser() {
		userEJB.updateUser(user);
	}

	@PostConstruct
	public void init() {
		user = currentUserManager.getCurrentAccount();
		isChanged = false;

	}

	/**
	 * 
	 */
	public profilController() {
		rdvs = new LazyDataModel<Rdv>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -1288141931737791259L;

			@Override
			public List<Rdv> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				List<Rdv> list = rdvEJB.findAllRdvUser(user, first, pageSize);
				rdvs.setRowCount(list.size());
				return list;
			}
		};

	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the isChanged
	 */
	public boolean isChanged() {
		return isChanged;
	}

	/**
	 * @param isChanged
	 *            the isChanged to set
	 */
	public void setChanged(boolean isChanged) {
		this.isChanged = isChanged;
	}

	/**
	 * @return the rdvs
	 */
	public LazyDataModel<Rdv> getRdvs() {
		return rdvs;
	}

	/**
	 * @return the selectedRdv
	 */
	public Rdv getSelectedRdv() {
		return selectedRdv;
	}

	/**
	 * @param selectedRdv
	 *            the selectedRdv to set
	 */
	public void setSelectedRdv(Rdv selectedRdv) {
		this.selectedRdv = selectedRdv;
	}

}
